//
//  LeaderFormCell.m
//  PropertyMananger
//
//  Created by Mansion on 17/3/4.
//  Copyright © 2017年 BH. All rights reserved.
//

#import "LeaderFormCell.h"

#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI) * 360

@implementation LeaderFormCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString * CellID = @"LeaderFormCellID";
    LeaderFormCell * cell = [tableView dequeueReusableCellWithIdentifier:CellID];
    if (!cell) {
        
        cell = [[LeaderFormCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        UILabel * tipLabel = [[UILabel alloc]init];
        [self.contentView addSubview:tipLabel];
        tipLabel.backgroundColor = BHColor(245, 95, 29);
        
        [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self.contentView).with.offset(15);
            make.top.equalTo(self.contentView).with.offset(15);
            make.size.mas_equalTo(CGSizeMake(2, 15));
        }];
        
        self.progressLabel = [[UILabel alloc]init];
        [self.contentView addSubview:self.progressLabel];
        self.progressLabel.text = @"本月工单进度";
        
        self.progressLabel.textAlignment = NSTextAlignmentLeft;
        self.progressLabel.font = BHFont(18);
        self.progressLabel.textColor = [UIColor blackColor];
        
        
        [self.progressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(tipLabel.mas_trailing).with.offset(5);
            make.centerY.equalTo(tipLabel).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(150, 25));
        }];
        
        //        self.detailBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        //        [self.contentView addSubview:self.detailBtn];
        //
        //        [self.detailBtn setTitle:@"明细 >>" forState:UIControlStateNormal];
        //        [self.detailBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //        self.detailBtn.titleLabel.font = BHFont(15);
        //
        //        [self.detailBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        //            make.trailing.equalTo(self.contentView).with.offset(-15);
        //            make.centerY.equalTo(tipLabel).with.offset(0);
        //            make.size.mas_equalTo(CGSizeMake(70, 30));
        //        }];
        
        self.monthButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:self.monthButton];
        [self.monthButton setImage:IMG(@"ico_statistics_month") forState:UIControlStateNormal];
        
        [self.monthButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.trailing.equalTo(self.contentView).with.offset(-15);
            make.centerY.equalTo(tipLabel).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(50, 50));
        }];
        
        self.yearLabel = [[UILabel alloc]init];
        [self.contentView addSubview:self.yearLabel];
        
        self.yearLabel.textAlignment = NSTextAlignmentRight;
        self.yearLabel.textColor = BHColor(102, 102, 102);
        self.yearLabel.font = BHFont(15);
        self.yearLabel.text = @"2017年/1月份";
        
        [self.yearLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.trailing.equalTo(self.monthButton.mas_leading).with.offset(0);
            make.centerY.equalTo(self.monthButton).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(BHscreenW/3+10, 25));
        }];
        
        
        
        [self setupCircleView];
        
        [self createProgressFormUI];
        
        [self createButton];
        
        
    }
    
    return self;
}

/**
 *  初始化圆形视图
 */
- (void)setupCircleView{
    
    //1.初始化

    self.circleView = [[FZHDrawCircleView alloc]init];
    [self.contentView addSubview:self.circleView];
    
    [self.circleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.contentView).with.offset(BHscreenW/2 - 80);
        make.top.equalTo(self.monthButton.mas_bottom).with.offset(110);
        make.size.mas_equalTo(CGSizeMake(100, 100));
    }];
    
    //2.彩色属性
    
    //    self.currentNum = 200.0;//彩色的数量
    //    self.totalNum = 600.0;//总比例
    
    self.circleView.colourFillColor = BHColor(65, 221, 195);//颜色
    self.circleView.normalFillColor = BHColor(221, 233, 244);
    self.circleView.colourStartAngle = 0;//起始点
    
    self.circleViewTwo = [[FZHDrawCircleView alloc]init];
    [self.contentView addSubview:self.circleViewTwo];
    
    [self.circleViewTwo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.contentView).with.offset(20);
        make.top.equalTo(self.circleView).with.offset(0);
        make.size.mas_equalTo(CGSizeMake(100, 100));
    }];
    
    //2.彩色属性
    self.circleViewTwo.colourFillColor = BHColor(255, 200, 68);//颜色
    self.circleViewTwo.normalFillColor = BHColor(221, 233, 244);
    self.circleViewTwo.colourStartAngle = 0;//起始点
    
    
}
- (void)createButton{

    self.leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.leftButton];
 //   self.leftButton.backgroundColor =[UIColor redColor];
    
    [self.leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.leading.equalTo(self.contentView).offset(0);
        make.top.equalTo(self.progressLabel.mas_bottom).offset(10);
        make.size.mas_equalTo(CGSizeMake(BHscreenW/2, 200));
    }];
    
    self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.rightButton];
//    self.rightButton.backgroundColor =[UIColor redColor];
    
    [self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.trailing.equalTo(self.contentView).offset(0);
        make.top.equalTo(self.progressLabel.mas_bottom).offset(10);
        make.size.mas_equalTo(CGSizeMake(BHscreenW/2, 200));
    }];

}
- (void)createProgressFormUI{
    
    UILabel * cubeLabel = [[UILabel alloc]init];
    [self.contentView addSubview:cubeLabel];
    cubeLabel.backgroundColor = BHColor(221, 233, 244);
    
    [cubeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.contentView).with.offset(25);
        make.top.equalTo(self.circleView).with.offset(50);
        make.size.mas_equalTo(CGSizeMake(10, 10));
    }];
    
    self.finishLabel = [[UILabel alloc]init];
    [self.contentView addSubview:self.finishLabel];
    
    // self.finishLabel.text = @"未完成:40单 25%";
    self.finishLabel.font = BHFont(13);
    self.finishLabel.textColor = BHColor(102, 102, 102);
    
    [self.finishLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(cubeLabel.mas_trailing).with.offset(5);
        make.centerY.equalTo(cubeLabel).with.offset(0);
        make.size.mas_equalTo(CGSizeMake(BHscreenW/2, 25));
    }];
    
    UIView * lineView = [[UIView alloc]init];
    [self.contentView addSubview:lineView];
    lineView.backgroundColor = BHGrayColor;
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.finishLabel).with.offset(0);
        make.top.equalTo(self.finishLabel.mas_bottom).with.offset(0);
        make.size.mas_equalTo(CGSizeMake(110, 1));
    }];
    
    
    UILabel * cubeLabel1 = [[UILabel alloc]init];
    [self.contentView addSubview:cubeLabel1];
    cubeLabel1.backgroundColor = BHColor(65, 221, 195);
    
    [cubeLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(cubeLabel).with.offset(0);
        make.top.equalTo(lineView).with.offset(10);
        make.size.mas_equalTo(CGSizeMake(10, 10));
    }];
    
    
    self.unFinishLabel = [[UILabel alloc]init];
    [self.contentView addSubview:self.unFinishLabel];
    
    //  self.unFinishLabel.text = @"已完成:140单 75%";
    self.unFinishLabel.font = BHFont(13);
    self.unFinishLabel.textColor = BHColor(102, 102, 102);
    
    [self.unFinishLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(cubeLabel1.mas_trailing).with.offset(5);
        make.centerY.equalTo(cubeLabel1).with.offset(0);
        make.size.mas_equalTo(CGSizeMake(BHscreenW/2, 25));
    }];
    
    
    //投诉
    UILabel * cubeLabel2 = [[UILabel alloc]init];
    [self.contentView addSubview:cubeLabel2];
    cubeLabel2.backgroundColor = BHColor(221, 233, 244);
    
    [cubeLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.contentView).with.offset(-135);
        make.top.equalTo(self.circleView).with.offset(50);
        make.size.mas_equalTo(CGSizeMake(10, 10));
    }];
    
    self.ComfinishLab = [[UILabel alloc]init];
    [self.contentView addSubview:self.ComfinishLab];
    
    //   self.ComfinishLab.text = @"未完成:40单 25%";
    self.ComfinishLab.font = BHFont(13);
    self.ComfinishLab.textColor = BHColor(102, 102, 102);
    
    [self.ComfinishLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(cubeLabel2.mas_trailing).with.offset(5);
        make.centerY.equalTo(cubeLabel2).with.offset(0);
        make.size.mas_equalTo(CGSizeMake(BHscreenW/2, 25));
    }];
    
    UIView * lineView1 = [[UIView alloc]init];
    [self.contentView addSubview:lineView1];
    lineView1.backgroundColor = BHGrayColor;
    
    [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.ComfinishLab).with.offset(0);
        make.top.equalTo(self.ComfinishLab.mas_bottom).with.offset(0);
        make.size.mas_equalTo(CGSizeMake(110, 1));
    }];
    
    
    UILabel * cubeLabel3 = [[UILabel alloc]init];
    [self.contentView addSubview:cubeLabel3];
    cubeLabel3.backgroundColor = BHColor(255, 200, 68);
    
    [cubeLabel3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(cubeLabel2).with.offset(0);
        make.top.equalTo(lineView1).with.offset(10);
        make.size.mas_equalTo(CGSizeMake(10, 10));
    }];
    
    
    self.ComUnFinishLab = [[UILabel alloc]init];
    [self.contentView addSubview:self.ComUnFinishLab];
    
    //  self.ComUnFinishLab.text = @"已完成:140单 75%";
    self.ComUnFinishLab.font = BHFont(13);
    self.ComUnFinishLab.textColor = BHColor(102, 102, 102);
    
    [self.ComUnFinishLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(cubeLabel3.mas_trailing).with.offset(5);
        make.centerY.equalTo(cubeLabel3).with.offset(0);
        make.size.mas_equalTo(CGSizeMake(BHscreenW/2, 25));
    }];
    
    
}
-(void)setModel:(LeaderModel *)model{
    
    _model = model;
  
    if ([model.notRepairall isEqualToString:@"0"] ||[model.notComplainall isEqualToString:@"0"] ) {
        
        self.circleView.colourEndAngle = DEGREES_TO_RADIANS(0.001/600.0);
        self.circleViewTwo.colourEndAngle = DEGREES_TO_RADIANS(0.001/600.0);
        
    }else if ([model.okRepairall isEqualToString:model.Repairall] || [model.okRepairall isEqualToString:model.Repairall]){
        
        self.circleView.colourEndAngle = DEGREES_TO_RADIANS(model.Repairall.floatValue/model.Repairall.floatValue);
        self.circleViewTwo.colourEndAngle = DEGREES_TO_RADIANS(model.Complainall.floatValue/model.Complainall.floatValue);
    }else{
        
        self.circleView.colourEndAngle = DEGREES_TO_RADIANS(model.okRepairall.floatValue/model.Repairall.floatValue);
        self.circleViewTwo.colourEndAngle = DEGREES_TO_RADIANS(model.okComplainall.floatValue/model.Complainall.floatValue);
    }

    if ([model.okRepairall isEqualToString:@"0"] || [model.okComplainall isEqualToString:@"0"]) {
        //当已完成未0的时候
       self.circleView.colourEndAngle = DEGREES_TO_RADIANS(0.001/600.0);
        self.circleViewTwo.colourEndAngle = DEGREES_TO_RADIANS(0.001/600.0);
        
    }
    
    self.circleView.moneyNumLabel.text = @"报修单";
    self.circleViewTwo.moneyNumLabel.text = @"投诉单";
    
    if (![model.Repairall isEqualToString:@"0"] || ![model.Complainall isEqualToString:@"0"]) {
    
        //报修单
        NSString *  unFinishPercent = [NSString stringWithFormat:@"%.f%%",(model.notRepairall.floatValue/model.Repairall.floatValue)*100];
        
        NSString * finishPercent = [NSString stringWithFormat:@"%.f%%",(model.okRepairall.floatValue/model.Repairall.floatValue)*100];
        
        self.finishLabel.text = [NSString stringWithFormat:@"未完成:%@单 %@",model.notRepairall,unFinishPercent];
        self.unFinishLabel.text = [NSString stringWithFormat:@"已完成:%@单 %@",model.okRepairall,finishPercent];
        //投诉单
        
        NSString * complainFinPersent = [NSString stringWithFormat:@"%.f%%",(model.notComplainall.floatValue/model.Complainall.floatValue)*100];
        NSString * complainUnFinPersent = [NSString stringWithFormat:@"%.f%%",(model.okComplainall.floatValue/model.Complainall.floatValue)*100];
        
        self.ComfinishLab.text = [NSString stringWithFormat:@"未完成:%@单 %@",model.notComplainall,complainFinPersent];
        self.ComUnFinishLab.text = [NSString stringWithFormat:@"已完成:%@单 %@",model.okComplainall,complainUnFinPersent];
        
        
        self.circleView.cateLabel.text = model.Repairall;
        self.circleViewTwo.cateLabel.text = model.Complainall;
        
    }else{
    
        //报修单
        NSString *  unFinishPercent = @"0%";
        
        NSString * finishPercent = @"0%";
        self.finishLabel.text = [NSString stringWithFormat:@"未完成:%@单 %@",@"0",unFinishPercent];
        self.unFinishLabel.text = [NSString stringWithFormat:@"已完成:%@单 %@",@"0",finishPercent];
        //投诉单
        
        NSString * complainFinPersent = @"0%";
        NSString * complainUnFinPersent = @"0%";
        
        self.ComfinishLab.text = [NSString stringWithFormat:@"未完成:%@单 %@",@"0",complainFinPersent];
        self.ComUnFinishLab.text = [NSString stringWithFormat:@"已完成:%@单 %@",@"0",complainUnFinPersent];
        
        self.circleView.cateLabel.text = @"0";
        self.circleViewTwo.cateLabel.text = @"0";
    }
    
    
    [self.circleViewTwo setNeedsDisplay];
    [self.circleView setNeedsDisplay];
    
}
@end
